package com.feng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.entity.RestResponse;
import com.feng.entity.vo.AddCommentVo;
import com.feng.entity.vo.ArticleListPageVo;
import com.feng.entity.vo.CommentVo;
import com.feng.exception.SystemException;
import com.feng.mapper.CommentMapper;
import com.feng.entity.Comment;
import com.feng.service.AccountService;
import com.feng.service.CommentService;
import com.feng.utils.Const;
import com.feng.utils.HttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Comment)表服务实现类
 *
 * @author feng
 * @since 2024-11-05 21:33:35
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    final
    AccountService accountService;

    public CommentServiceImpl(AccountService accountService) {
        this.accountService = accountService;
    }

    @Override
    public RestResponse commentList(Integer type, String articleId, Integer pageCurrent, Integer pageSize) {
        //条件 文章id相同 并且时根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Const.ZERO == type, Comment::getArticleId, articleId)
                .eq(Comment::getRootId, -1) //设置查询根评论
                .eq(Comment::getType, type) //评论类型
                .orderByDesc(Comment::getCreateTime); //按创建时间降序排列
        Page page = new Page(pageCurrent, pageSize);
        page(page, queryWrapper);

        //转换Vo并添加用户昵称和被回复的用户昵称
        List<CommentVo> commentVos = toCommentVoList(page.getRecords());

        //通过查询出来的根评论去查询根评论的子评论
        commentVos.stream()
                .map(commentVo -> {
                    LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(Comment::getRootId, commentVo.getId())
                            .orderByAsc(Comment::getCreateTime);
                    List<Comment> list = list(wrapper);
                    commentVo.setChildren(toCommentVoList(list));
                    return commentVo;
                })
                .collect(Collectors.toList());
        return RestResponse.success(new ArticleListPageVo(commentVos, page.getTotal()));
    }

    @Transactional
    @Override
    public RestResponse addComment(AddCommentVo addComment) {
        if(!StringUtils.hasText(addComment.getContent())){
            throw new SystemException(HttpCodeEnum.COMMENT_NOT_NULL);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(addComment, comment);
        getBaseMapper().AUTO_INCREMENT();
        save(comment);
        return RestResponse.success();
    }

    //用来转换CommentVo并添加用户昵称和被回复的用户昵称
    public List<CommentVo> toCommentVoList(List<Comment> comments){
        List<CommentVo> commentVos = new ArrayList<>();
        comments.stream()
                        .forEach(comment -> commentVos.add(BeanUtil.copyProperties(comment, CommentVo.class)));
        commentVos.stream()
                .map(commentVo -> {
                    String nickName = accountService.getById(commentVo.getCreateBy()).getNickname();
                    commentVo.setNickname(nickName);
                    if (!commentVo.getToCommentUserId().equals("-1")) {
                        String toCommentUserName = accountService.getById(commentVo.getToCommentUserId()).getNickname();
                        commentVo.setToCommentUserName(toCommentUserName);
                    }
                    String avatar = accountService.getById(commentVo.getCreateBy()).getAvatar();
                    if (avatar != null){
                        commentVo.setAvatar(avatar);
                    }
                    return commentVo;
                })
                .collect(Collectors.toList());
        return commentVos;
    }
}

